package com.czh.springboot04.MyController;

import com.czh.springboot04.entity.SystemUser;
import com.czh.springboot04.entity.User;
import com.czh.springboot04.service.UserService;
import com.czh.springboot04.util.MD5Util;
import com.czh.springboot04.util.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
//@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;


    @GetMapping("/findUserByUsername")
    public R findUserByUsername(@RequestParam("username") String username){
        System.out.println(username);
        User user = userService.findByUsername(username);
        System.out.println(user);
        if(user!=null)
            return R.ok(user);
        else
            return R.fail("用户不存在");
    }


    @RequestMapping("selectAll")
    public R selectAll(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum, @RequestParam(value = "pageSize",required = false,defaultValue = "2")int pageSize, User user){
        //System.out.println(user);
        PageInfo<User> userPageInfo = userService.selectAll(pageNum, pageSize, user);
        return R.ok(userPageInfo);
    }

    @GetMapping("getUserCount")
    public R getUserCount(){
        int i = userService.getUserCount();
        return R.ok(i);
    }

    @PostMapping("addUser")
    public R addUser(@RequestBody User user){
        if(user==null)
            return R.fail("添加失败");
        user.setPassword(MD5Util.encode(user.getUsername()+user.getPassword()));
        int i = userService.addUser(user);
        if(i>0)
            return R.ok(user);
        else
            return R.fail("添加失败");
    }

    @RequestMapping("/login")
    public R login(@RequestBody User user) {
        user.setPassword(MD5Util.encode(user.getUsername()+user.getPassword()));
        User u1 = userService.findByUsernameAndPassword(user);
        System.out.println(u1);
        if(u1 == null)
            return R.fail("用户名或密码错误");
        else
            return R.ok(u1);
    }

    @DeleteMapping("/{id}")
    public R del(@PathVariable("id") int id) {
        int i = userService.deleteUser(id);
        if (i > 0)
            return R.ok("删除成功");

        return R.fail("删除失败");
    }

    @PutMapping("/editPassword")
    public R editPassword(@RequestBody User user){
        //System.out.println(systemUser);
        user.setPassword(MD5Util.encode(user.getUsername()+user.getPassword()));

        int edit = userService.updateU(user);
        return R.ok(edit);
    }

    @PutMapping("/updateU")
    public R updateU(@RequestBody User user){
        int i = userService.updateU(user);
        if(i>0)
            return R.ok("修改成功");
        else
            return R.fail("修改失败");
    }
    @GetMapping("/{id}")
    public R findById(@PathVariable("id") int id){
        User user = userService.selectById(id);
        return R.ok(user);
    }
}
